package org.dczl.system.controller.daping;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import lombok.extern.slf4j.Slf4j;
import org.dczl.common.core.exception.ServiceException;
import org.dczl.system.domain.vo.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dczl.common.idempotent.annotation.RepeatSubmit;
import org.dczl.common.web.core.BaseController;
import org.dczl.common.core.domain.R;
import org.dczl.common.core.validate.AddGroup;
import org.dczl.common.core.validate.EditGroup;
import org.dczl.system.domain.bo.DpLabBo;
import org.dczl.system.service.IDpLabService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实验室
 * 前端访问路由地址为:/core/lab
 *
 * @author jia
 * @date 2025-04-16
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lab")
public class DpLabController extends BaseController {

    private final IDpLabService dpLabService;

    @Value("${upload.route}")
    private String uploadRoute;

    @Value("${upload.url}")
    private String uploadUrl;

    /**
     * 查询大屏数据
     */
    @GetMapping("/daPing")
    public R<List<DpLabVo>> daPing() {
        return R.ok(dpLabService.daPing());
    }

    /**
     * 查询实验记录数据
     */
    @GetMapping("/records")
    public R<List<JSONObject>> records() {
        return R.ok(JSONUtil.toList("[{\"student_name\":\"王同学\",\"dev_name\":\"设备1\",\"exp_name\":\"液位单闭环控制实验1\",\"exp_stu\":0,\"exp_start_time\":\"2025-01-02 08:00:00\",\"exp_end_time\":\"2025-01-04 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":24.33},{\"name\":\"下水箱液位\",\"value\":12.22},{\"name\":\"管路压力\",\"value\":33.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"},{\"student_name\":\"张同学\",\"dev_name\":\"设备2\",\"exp_name\":\"液位单闭环控制实验2\",\"exp_stu\":1,\"exp_start_time\":\"2025-01-01 08:00:00\",\"exp_end_time\":\"2025-01-01 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":22.33},{\"name\":\"下水箱液位\",\"value\":11.22},{\"name\":\"管路压力\",\"value\":33.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"},{\"student_name\":\"李同学\",\"dev_name\":\"设备3\",\"exp_name\":\"液位单闭环控制实验3\",\"exp_stu\":1,\"exp_start_time\":\"2025-01-01 08:00:00\",\"exp_end_time\":\"2025-01-01 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":12.33},{\"name\":\"下水箱液位\",\"value\":15.22},{\"name\":\"管路压力\",\"value\":23.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"}" +
                ",{\"student_name\":\"王同学1\",\"dev_name\":\"设备4\",\"exp_name\":\"液位单闭环控制实验4\",\"exp_stu\":0,\"exp_start_time\":\"2025-01-01 08:00:00\",\"exp_end_time\":\"2025-01-01 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":22.33},{\"name\":\"下水箱液位\",\"value\":11.22},{\"name\":\"管路压力\",\"value\":33.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"},{\"student_name\":\"张同学1\",\"dev_name\":\"设备5\",\"exp_name\":\"液位单闭环控制实验5\",\"exp_stu\":1,\"exp_start_time\":\"2025-01-01 08:00:00\",\"exp_end_time\":\"2025-01-01 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":33.33},{\"name\":\"下水箱液位\",\"value\":51.22},{\"name\":\"管路压力\",\"value\":73.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"},{\"student_name\":\"李同学1\",\"dev_name\":\"设备6\",\"exp_name\":\"液位单闭环控制实验6\",\"exp_stu\":0,\"exp_start_time\":\"2025-01-01 08:00:00\",\"exp_end_time\":\"2025-01-01 08:40:00\",\"curr_data\":[{\"name\":\"上水箱液位\",\"value\":22.33},{\"name\":\"下水箱液位\",\"value\":12.22},{\"name\":\"管路压力\",\"value\":63.44}],\"flv_addr\":\"https://dp.chichaqu1.com/download/5e4e4681-db7d-4d96-8cc5-f45ae747244f.mp4\"}]", JSONObject.class));
    }

    /**
     * 查询大屏实验室占比
     */
    @GetMapping("/ratio")
    public R<List<DpRatioVo>> ratio() {
        return R.ok(dpLabService.ratio());
    }

    /**
     * 后台查询实验室列表
     */
    @GetMapping("/list")
    public R<List<DpLabVo>> list() {
        return R.ok(dpLabService.list());
    }

    /**
     * 查询天气
     */
    @GetMapping("/weather")
    public R<DpWeatherVo> weather() {
        return R.ok(dpLabService.weather());
    }

    /**
     * 文件上传
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<DpUploadVo> handleFileUpload(@RequestParam("file") MultipartFile file) {
        try {
            DpUploadVo uploadVo = new DpUploadVo();
            String originalFilename = file.getOriginalFilename();
            String type = FileUtil.extName(originalFilename);
            if (Validator.isEmpty(type)) {
                throw new ServiceException("文件格式不正确");
            }
            String name = IdUtil.randomUUID() + StrUtil.DOT + type;
            File uploadedFile = new File(uploadRoute + name);
            file.transferTo(uploadedFile);
            uploadVo.setUrl(uploadUrl + name);
            uploadVo.setFileName(originalFilename);
            return R.ok(uploadVo);
        } catch (IOException e) {
            log.error("文件上传失败", e);
            throw new ServiceException("上传失败!");
        }
    }


    /**
     * 新增实验室
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DpLabBo bo) {
        return toAjax(dpLabService.insertByBo(bo));
    }

    /**
     * 修改实验室
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DpLabBo bo) {
        return toAjax(dpLabService.updateByBo(bo));
    }

    /**
     * 删除实验室
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dpLabService.deleteWithValidByIds(List.of(ids), true));
    }

}
